<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/3/3
 * Time: 15:10
 */

namespace app\admin\service;


use app\common\model\mysql\AuthGroup;

class GroupService
{
    protected $groupModel = null;

    public function __construct()
    {
        $this->groupModel = new AuthGroup();
    }

    public function addGroup($data)
    {
        $result = $this->groupModel->addGroup($data);
        if (false === $result) {
            throw new \think\Exception("角色添加失败");
        }

        return true;
    }

    public function getAllGroups()
    {
        $result = $this->groupModel->getAllGroups();
        if (empty($result)) {
            return [];
        }

        $result = $result->toArray();

        return $result;
    }

    public function getGroupInfoById($id)
    {
        $result = $this->groupModel->getGroupInfoById($id);
        if (empty($result)) {
            throw new \think\Exception("获取角色信息失败");
        }

        return $result;
    }

    public function updateGroupInfoById($id, $data)
    {
        $result = $this->groupModel->updateGroupInfoById($id, $data);
        if (false === $result) {
            throw new \think\Exception("更新角色信息失败");
        }

        return true;
    }

    /**
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    public function deleteById($id)
    {
        $res = $this->groupModel->deleteById($id);
        if (false === $res) {
            throw new \think\Exception("角色删除失败");
        }

        return true;
    }
}